package com.api.smoke.invitation;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.api.test.VoidContextLoader;
import com.api.util.AssertUtil;
import com.api.util.DataUtil;
import com.api.util.GenericUtils.GenericType;
import com.api.util.repo.DtoFactory;
import com.api.util.repo.impl.DomainUser;
import com.util.zyncroweb.UserStatus;
import com.zyncro.api.IInvitationApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.InvitationApi;
import com.zyncro.api.model.InvitationApiType;
import com.zyncro.api.model.InvitationRoleApiType;
import com.zyncro.api.model.InvitationStateApiType;
import com.zyncro.api.model.PagedItemsResultApi;

@ContextConfiguration(loader = VoidContextLoader.class)
public class GetInvitationsSmoke extends AbstractApiTest {

	private PagedItemsResultApi<InvitationApi> expectedResults;

	@BeforeClass
	protected void setUp() throws Exception {

		final String domainName = this.getClass().getSimpleName();
		final DomainUser mainAdmin = getRepo().getMainUser(domainName);

		getApi().login(mainAdmin.getAppId(), mainAdmin.getPassword());

		String fileName = getRepo().getPropertyDto(domainName, new DtoFactory<String>() {

			public String createDto() {

				getZyncroWebApi().login(mainAdmin.getAppId(), mainAdmin.getPassword());
				getZyncroWebApi().setUserStatus(UserStatus.BUSY, "Estoy ocupado");

				String env = System.getProperty("env");

				String fileName = String.format("./json/%s/%s.json", env, domainName);

				try {
					PagedItemsResultApi<InvitationApi> result = getApi().getService(IInvitationApiService.class).getInvitations(0, 0, null,
							null, null);

					DataUtil.saveDtoToFile(result, fileName);

				} catch (Exception e) {

					throw new IllegalStateException("Exception creating test data", e);
				}
				return fileName;
			}
		});

		this.expectedResults = DataUtil.getDtoFromFile(fileName, new GenericType<PagedItemsResultApi<InvitationApi>>() {
		});
	}

	@Test
	public void testGetInvitations() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<InvitationStateApiType> invitationStates = null;
		List<InvitationApiType> invitationTypes = null;
		InvitationRoleApiType invitationRole = null;

		PagedItemsResultApi<InvitationApi> result = getApi().getService(IInvitationApiService.class).getInvitations(pageNumber,
				itemsPerPage, invitationStates, invitationTypes, invitationRole);

		Assert.assertEquals(result.getPageNumber(), 1, "pageNumber");
		Assert.assertEquals(result.getTotalItems(), expectedResults.getTotalItems());

		Collection<InvitationApi> resultElements = result.getElements();
		Collection<InvitationApi> expectedElements = expectedResults.getElements();

		Iterator<InvitationApi> it = expectedElements.iterator();
		for (InvitationApi groupApi : resultElements) {
			InvitationApi expectedGroup = it.next();
			AssertUtil.assertDTOEquals(groupApi, expectedGroup);
		}
	}
}
